package com.joysuch.wwyt.attendance.controller;


import com.joysuch.wwyt.attendance.dto.AttendanceGroupDto;
import com.joysuch.wwyt.attendance.entity.AttendanceGroup;
import com.joysuch.wwyt.attendance.service.AttendanceGroupService;
import com.joysuch.wwyt.attendance.vo.AttendanceGroupVo;
import com.joysuch.wwyt.core.bean.SimpleListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 考勤班组
 * </p>
 *
 * @author zqw
 * @since 2022-03-01
 */
@RestController
@RequestMapping("/attendance/group")
@Api(tags = "考勤组")
@Slf4j
public class AttendanceGroupController {

    @Autowired
    private AttendanceGroupService groupService;


    @PostMapping("save")
    @ApiOperation("保存考勤组")
    @RequiresPermissions("attendance:group:save")
    public ResultBean save(@Valid @RequestBody AttendanceGroupDto dto) {
        AttendanceGroup group = null;
        try {
            group = groupService.saveGroup(dto);
            return ResultBean.success(group);
        } catch (ApiBusinessException e) {
            return ResultBean.wrapErrorResult(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("findById")
    @ApiOperation("查看考勤组")
    public ResultBean findById(@RequestParam(value = "id") Long id) {
        AttendanceGroupVo bean = groupService.findById(id);
        return ResultBean.success(bean);
    }

    @GetMapping("getList")
    @ApiOperation("查看考勤组列表")
    public ResultBean findById() {
        List<SimpleListBean> list = groupService.getList();
        return ResultBean.success(list);
    }

    @GetMapping("deleteById")
    @ApiOperation("删除考勤组")
    @RequiresPermissions("attendance:group:deleteById")
    public ResultBean deleteById(@RequestParam(value = "id") Long id) {
        try {
            groupService.deleteById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.wrapErrorResultDefault("删除失败");
        }
        return ResultBean.success("");
    }

}

